package com.jingdianjichi.subject.application.controller;

import com.alibaba.fastjson.JSON;
import com.google.common.base.Preconditions;
import com.jingdianjichi.subject.application.convert.SubjectCategoryDTOConverter;
import com.jingdianjichi.subject.application.dto.SubjectCategoryDTO;
import com.jingdianjichi.subject.common.entity.Result;
import com.jingdianjichi.subject.common.enums.IsDeletdFlageEnum;
import com.jingdianjichi.subject.domain.convert.SubjectCategoryConverter;
import com.jingdianjichi.subject.domain.entity.SubjectCategoryBO;
import com.jingdianjichi.subject.domain.service.SubjectCategoryDomainService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Hashtable;
import java.util.List;

/**
 * SubjectCategoryController 类
 *
 * @author 熊苏鹏1
 * @date 2025/1/30 17:42
 * @description 无
 */
@RestController
@Slf4j
@RequestMapping("/subject/category")
public class SubjectCategoryController {
    @Resource
    private SubjectCategoryDomainService subjectCategoryDomainService;

    /**
     * 新增分类
     * @param subjectCategoryDTO
     * @return
     */
    @PostMapping("/add")
    public Result<Boolean> add(@RequestBody SubjectCategoryDTO subjectCategoryDTO) {
        try {

            if (log.isInfoEnabled())
                log.info("SubjectCategoryController add dto:{}", JSON.toJSONString(subjectCategoryDTO));
            Preconditions.checkNotNull(subjectCategoryDTO.getCategoryType(), "分类类型不能为空");
            Preconditions.checkNotNull(subjectCategoryDTO.getCategoryName(), "分类名称不能为空");
            Preconditions.checkNotNull(subjectCategoryDTO.getParentId(), "分类父id不能为空");
            SubjectCategoryBO subjectCategoryBO = SubjectCategoryDTOConverter.INSTANCE.convertCategoryDTOToBo(subjectCategoryDTO);

            subjectCategoryDomainService.add(subjectCategoryBO);
            return Result.ok(true);
        } catch (Exception e) {
            log.error("SubjectCategoryController add error:{}", e.getMessage(), e);
            return Result.fail(false);

        }
    }

    /**
     * 获取最大的分类（例如前端，后端这种）
     * @return
     */

    @PostMapping("/queryPrimaryCategory")
    public Result queryPrimaryCategory(@RequestBody SubjectCategoryDTO subjectCategoryDTO) {
        try {
            SubjectCategoryBO subjectCategoryBO = SubjectCategoryDTOConverter.INSTANCE.convertCategoryDTOToBo(subjectCategoryDTO);
            List<SubjectCategoryBO> subjectCategoryBOList = subjectCategoryDomainService.queryPrimaryCategory(subjectCategoryBO);
            if (log.isInfoEnabled())
                log.info("SubjectCategoryController query bo:{}", JSON.toJSONString(subjectCategoryBO));
            List<SubjectCategoryDTO> subjectCategoryDTOList = SubjectCategoryDTOConverter.INSTANCE.convertCategoryBoToDTOList(subjectCategoryBOList);
            if (log.isInfoEnabled())
                log.info("SubjectCategoryController query dto:{}", JSON.toJSONString(subjectCategoryDTOList));
            return Result.ok(subjectCategoryDTOList);
        } catch (Exception e) {
            log.error("SubjectCategoryController queryPrimaryCategory error:{}", e.getMessage(), e);
            return Result.fail("查询失败");
        }

    }

    /**
     * 获取大分类下的子分类（例如Spring,SpringMVC这种）
     * @param subjectCategoryDTO
     * @return
     */

    @PostMapping("/queryCategoryByPrimary")
    public Result queryCategoryByPrimary(@RequestBody SubjectCategoryDTO subjectCategoryDTO) {
        try {
            SubjectCategoryBO subjectCategoryBO = SubjectCategoryDTOConverter.INSTANCE.convertCategoryDTOToBo(subjectCategoryDTO);
            Preconditions.checkNotNull(subjectCategoryBO.getParentId(), "父id不能为空");
            List<SubjectCategoryBO> boList = subjectCategoryDomainService.queryPrimaryCategory( subjectCategoryBO);
            List<SubjectCategoryDTO> subjectCategoryDTOList = SubjectCategoryDTOConverter.INSTANCE.convertCategoryBoToDTOList(boList);
            if (log.isInfoEnabled())
                log.info("SubjectCategoryController query2 bo:{}", JSON.toJSONString(boList));
      if (log.isInfoEnabled())
                log.info("SubjectCategoryController query2 dto:{}", JSON.toJSONString(subjectCategoryDTOList));
            return Result.ok(subjectCategoryDTOList);
        } catch (Exception e) {
            log.error("SubjectCategoryController queryPrimaryCategory error:{}", e.getMessage(), e);
            return Result.fail("查询失败");
        }
    }
    /**
     * 获取大分类下的子分类（例如Spring,SpringMVC这种）
     * @param subjectCategoryDTO
     * @return
     */

    @PostMapping("/Id")
    public Result queryCategoryAndLabel(@RequestBody SubjectCategoryDTO subjectCategoryDTO) {
        try {
            SubjectCategoryBO subjectCategoryBO = SubjectCategoryDTOConverter.INSTANCE.convertCategoryDTOToBo(subjectCategoryDTO);
            Preconditions.checkNotNull(subjectCategoryBO.getId(), "父id不能为空");
            List<SubjectCategoryBO> boList = subjectCategoryDomainService.queryCategoryAndLabel( subjectCategoryBO);
            List<SubjectCategoryDTO> subjectCategoryDTOList = SubjectCategoryDTOConverter.INSTANCE.convertCategoryBoToDTOList(boList);

            return Result.ok(subjectCategoryDTOList);
        } catch (Exception e) {
            log.error("SubjectCategoryController queryPrimaryCategory error:{}", e.getMessage(), e);
            return Result.fail("查询失败");
        }
    }

    /**
     * 更新分类
     * @param subjectCategoryDTO
     * @return
     */
    @PostMapping("/update")
    public Result update(@RequestBody SubjectCategoryDTO subjectCategoryDTO) {
        try {
            if (log.isInfoEnabled())
                log.info("SubjectCategoryController update bo:{}", JSON.toJSONString(subjectCategoryDTO));
            SubjectCategoryBO subjectCategoryBO = SubjectCategoryDTOConverter.INSTANCE.convertCategoryDTOToBo(subjectCategoryDTO);
            Boolean result = subjectCategoryDomainService.update(subjectCategoryBO);
            return Result.ok(result);
        } catch (Exception e) {
            log.error("SubjectCategoryController queryPrimaryCategory error:{}", e.getMessage(), e);
            return Result.fail("更新分类失败");
        }

    }
    @PostMapping("/delete")
    public Result delete(@RequestBody SubjectCategoryDTO subjectCategoryDTO) {
        try {
            if (log.isInfoEnabled())
                log.info("SubjectCategoryController delete bo:{}", JSON.toJSONString(subjectCategoryDTO));
            SubjectCategoryBO subjectCategoryBO = SubjectCategoryDTOConverter.INSTANCE.convertCategoryDTOToBo(subjectCategoryDTO);
            Boolean result = subjectCategoryDomainService.delete(subjectCategoryBO);
            return Result.ok(result);
        } catch (Exception e) {
            log.error("SubjectCategoryController queryPrimaryCategory error:{}", e.getMessage(), e);
            return Result.fail("删除分类失败");
        }

    }



}
